/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectUnit;
import DTO.MonthlyReportDetail_DTO;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Nguyen Phi Hung
 */
public class MonthlyReportDetail_BLL {
    
    ConnectUnit connection;
    
    public MonthlyReportDetail_BLL(){
        this.connection = DAL.DAL.getDAL();
    }
    
    /**
     * get list of MonthlyReportDetail form database with Condition and OrderBy
     * @param condition
     * @param OrderBy
     * @return
     * @throws Exception 
     */
    public HashSet<MonthlyReportDetail_DTO> getMonthlyReportDetails(String condition, String OrderBy) throws Exception{
        HashSet<MonthlyReportDetail_DTO> monthlyRDs = new HashSet<MonthlyReportDetail_DTO>();
        ResultSet result = this.connection.Select("MONTHLYREPORTDETAIL", condition, OrderBy);
        while(result.next()){
            MonthlyReportDetail_DTO monthlyRD = new MonthlyReportDetail_DTO();
            
            monthlyRD.setDetailID(result.getString("DetailID"));
            monthlyRD.setReportID(result.getString("ReportID"));
            monthlyRD.setReportingDate(result.getDate("ReportingDateb"));
            monthlyRD.setTotalOfClose(result.getInt("TotalOfOpen"));
            monthlyRD.setTotalOfOpen(result.getInt("TotalOfClose"));
            
            monthlyRDs.add(monthlyRD);
        }
        return monthlyRDs;
    }
    
    /**
     * get list of MonthlyReportDetail form database with Condition
     * @param condition
     * @return
     * @throws Exception 
     */
    public HashSet<MonthlyReportDetail_DTO> getMonthlyReportDetails(String condition) throws Exception{
        return this.getMonthlyReportDetails(condition, null);
    }
    
    /**
     * get list of MonthlyReportDetail form database
     * @return
     * @throws Exception 
     */
    public HashSet<MonthlyReportDetail_DTO> getMonthlyReportDetails() throws Exception{
        return this.getMonthlyReportDetails(null, null);
    }
    
    /**
     * get MonthlyReportDetail by DetailID from database
     * @param ID
     * @return
     * @throws Exception 
     */
    public MonthlyReportDetail_DTO getMonthlyReportDetail_byDetailID(String ID) throws Exception{
        HashSet<MonthlyReportDetail_DTO> monthlyRDs = this.getMonthlyReportDetails("DetailID= "+ ID, null);
        if(monthlyRDs.size()>=0)
           return monthlyRDs.toArray(new MonthlyReportDetail_DTO[monthlyRDs.size()])[0];
        return null;     
    }
    
    /**
     * insert a MonthlyReportDetail into database
     * @param info
     * @throws Exception 
     */
    public void Insert(MonthlyReportDetail_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        //put info into HashMap
        map.put("DetailID", info.getDetailID());
        map.put("ReportID", info.getReportID());
        map.put("ReportingDate", info.getReportingDate());
        map.put("TotalOfOpen", info.getTotalOfOpen());
        map.put("TotalOfClose", info.getTotalOfClose());
        
        String[] arr = {"DetailID","ReportID","ReportingDate","TotalOfOpen","TotalOfClose"}; 
        //insert map into database
        this.connection.Insert("MONTHLYREPORTDETAIL", map, arr);
    }
    
    /**
     * Update a MonthlyReportDetail in Database
     * @param info
     * @throws Exception 
     */
    public void Update(MonthlyReportDetail_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        //put info into HashMap
        map.put("DetailID", info.getDetailID());
        map.put("ReportID", info.getReportID());
        map.put("ReportingDate", info.getReportingDate());
        map.put("TotalOfOpen", info.getTotalOfOpen());
        map.put("TotalOfClose", info.getTotalOfClose());
        //update map into database
        this.connection.Update("MONTHLYREPORTDETAIL", map, "DetailID="+ info.getDetailID());
    }
    
    /**
     * Delete a MonthlyReportDetail in database
     * @param info
     * @throws Exception 
     */
    public void Delete(MonthlyReportDetail_DTO info) throws Exception{
        this.connection.Delete("MONTHLYREPORTDETAIL", "DetailID="+ info.getDetailID());
    }
}
